package com.wjbgn.electronicnian.service.impl;

import com.wjbgn.electronicnian.convert.NianDoConvert;
import com.wjbgn.electronicnian.dto.NianDTO;
import com.wjbgn.electronicnian.dto.Result;
import com.wjbgn.electronicnian.entity.NianDumplingsDO;
import com.wjbgn.electronicnian.enums.CommonReturnEnum;
import com.wjbgn.electronicnian.service.NianDumplingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjbgn.electronicnian.entity.NianDO;
import com.wjbgn.electronicnian.service.NianService;
import com.wjbgn.electronicnian.mapper.NianMapper;

import java.util.List;

/**
 * Description:
 * Create Date: 2022-01-20T11:06:12.859722400
 *
 * @author weirx
 * @version 1.0
 */
@Service
public class NianServiceImpl extends ServiceImpl<NianMapper, NianDO> implements NianService {

    @Autowired
    private NianDumplingsService dumplingsService;

    @Override
    public NianDO createNian(Long id, String username) {
        NianDO nianDO = new NianDO(id, "「" + username + "的小老虎」", 100);
        boolean save = this.save(nianDO);
        if (save) {
            return nianDO;
        } else {
            return null;
        }
    }

    @Override
    public Result getNian(Long id) {
        NianDO nianDO = this.getById(id);
        if (nianDO.getHungryNum() == 0){
            return Result.success(CommonReturnEnum.SO_HUNGRY_BYE.getCode(),CommonReturnEnum.SO_HUNGRY_BYE.getName(),null);
        }
        List<NianDumplingsDO> list = dumplingsService.lambdaQuery().eq(NianDumplingsDO::getNianId, id).list();
        int sum = list.stream().mapToInt(NianDumplingsDO::getDumplingsNum).sum();

        NianDTO nianDTO = NianDoConvert.doToDto(nianDO);
        nianDTO.setDumplingsNum(sum);
        return Result.success(nianDTO);
    }
}